package tree;

public class 求根到叶子节点数字之和_129 {
    
    private int sum = 0;

    public int sumNumbers(TreeNode root) {
        if(root != null){
           dfs(root,0);
        }
        return sum;
    }

    private void dfs(TreeNode root, int cur) {
        cur =  cur * 10 + root.val;
        if(root.left == null && root.right == null) { // 树的遍历终止条件一般是  root.left == null && root.right == null 需要记忆
            sum += cur; 
            return;
        }
        if(root.left != null) {
            dfs(root.left, cur);
        }
        if(root.right != null) {
            dfs(root.right, cur);
        }
    }
}
